package com.dwjm.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ConnectionDatabaseForMySQLUseJdbc {
    //连接数据库的驱动
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    //连接数据库的URL
    private static final String URL = "jdbc:mysql://localhost:3306/campus_dormitory_management?&useSSL=false&serverTimezone=Asia/Shanghai";
    //连接数据库的账号
    private static final String USER = "root";
    //连接数据库的密码
    private static final String PASSWORD = "123456";

    //定义数据库连接对象
    static Connection connection = null;

    //连接数据库的函数
    public static Connection getConnection() {
        try {
            //加载数据库驱动
            Class.forName(DRIVER);
            //我们期望要执行的程序，连接数据库的程序
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //测试数据库是否可以连接
    public static void main(String[] args) {System.out.println(getConnection() == null);
    }

    public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
    }
}
